package com.ruoyi.face.controller;

import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.utils.StringUtils;
import com.ruoyi.face.domain.SysUserFace;
import com.ruoyi.face.domain.vo.FaceVerifyResult;
import com.ruoyi.face.service.IFaceService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;

/**
 * 人脸识别控制器
 *
 * @author ruoyi
 */
@RestController
@RequestMapping("/face")
@Api(tags = "人脸识别接口")
public class FaceController {
    @Autowired
    private IFaceService faceService;

    /**
     * 人脸注册
     */
    @PostMapping("/register")
    @ApiOperation("人脸注册")
    public R<SysUserFace> registerFace(@RequestParam Long userId, @RequestParam String faceImage) {
        if (userId == null || StringUtils.isEmpty(faceImage)) {
            return R.fail("用户ID和人脸图片不能为空");
        }
        SysUserFace userFace = faceService.registerFace(userId, faceImage);
        return R.ok(userFace);
    }

    /**
     * 人脸验证
     */
    @PostMapping("/verify")
    @ApiOperation("人脸验证")
    public R<FaceVerifyResult> verifyFace(@RequestParam Long userId, @RequestParam String faceImage) {
        if (userId == null || StringUtils.isEmpty(faceImage)) {
            return R.fail("用户ID和人脸图片不能为空");
        }
        FaceVerifyResult result = faceService.verifyFace(userId, faceImage);
        return R.ok(result);
    }

    /**
     * 查询用户人脸列表
     */
    @GetMapping("/list/{userId}")
    @ApiOperation("查询用户人脸列表")
    public R<List<SysUserFace>> getUserFaceList(@PathVariable Long userId) {
        if (userId == null) {
            return R.fail("用户ID不能为空");
        }
        List<SysUserFace> faceList = faceService.getFaceListByUserId(userId);
        return R.ok(faceList);
    }

    /**
     * 删除人脸信息
     */
    @DeleteMapping("/{faceId}")
    @ApiOperation("删除人脸信息")
    public R<Boolean> deleteFace(@PathVariable Long faceId) {
        if (faceId == null) {
            return R.fail("人脸ID不能为空");
        }
        boolean success = faceService.deleteFace(faceId);
        return success ? R.ok(true) : R.fail("删除失败");
    }
}